/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tletlis.network;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.net.*;
import java.io.*;
import java.util.*;

/**
 * A Threaded class that together with the classes SendMessage and StreamListener will
 * act as a Server on a Client-Server network.
 * The Server class will listen after a given number of clients to connect
 * on a given port.
 * When a new client has connected, a new StreamListener is created that will
 * listen after messages from the client to server.
 * The server also contains a list of all connected clients.
 * @author Max
 */

public class Server extends Thread {

    /**
     * The port that server is listening on
     */
    private int port;

    /**
     * A List that contains all connected client sockets
     */
    private List<Socket> clientSocketList= new ArrayList<Socket>();

   /**
    * The ServerSocket
    */
    private ServerSocket servSocket;

    /**
     * The number of clients expected to connect
     */
    private int numbClients;

    /**
     * The constructor initializes values 
     * and starts new Thread.
     * A new Server object is created.
     *
     * @param port
     * @param numbClients
     */
    public Server(int port, int numbClients ) {
        this.port=port;
        this.numbClients=numbClients;    
        this.start();
    }
    
    /**
     * Method that returns a "clone" of clientSocketList
     * @return clientSocketList
     */
    public List<Socket> getClientSocketList(){
        List<Socket >s= new ArrayList<Socket>(clientSocketList);
        return s;
    }
            


    @Override
    /**
     * The overridden run method.
     * The method will listen after a number of clients to connect on a given
     * port. When a new client has conected the method will add the connected
     * client to the clientSocketList. It will also create a new StreamListener
     * for the connected client.
     */
     public void run() {

        try {
            servSocket = new ServerSocket(port);
            System.out.println("Server is listening for clients..");

            //Loop that will add the given number of clients to clientSocketList
            for(int i = 0; i < numbClients ;i++){

                //Waiting for new client to connect
                Socket tempSock =servSocket.accept();
                //client has connected, add to List
                clientSocketList.add(tempSock);
                //make new StreamListener for the new client
                //StreamListener listens for messages sent to server from
                //given client
                new StreamListener(tempSock , this);
                System.out.println(tempSock.getInetAddress().getHostName()+" is connected"); 
            }
            //All clients has connected
            System.out.println("All "+ clientSocketList.size() +" clients has successfully connected");

            //send a message to all clients so that they know that all clinets
            //has connected
            new SendMessage("start", clientSocketList);
        }
        catch (IOException ex) {
            System.out.println("IOException in establish server method");
        }

       
    }



}
